<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/8/21 14:11
 */

namespace App\Controller\User;


use App\Controller\ApiController;
use App\Request\User\CategoryDataRequest;
use App\Request\User\CategoryEditRequest;
use App\Request\User\CategoryRemoveRequest;
use App\Request\User\CategorySaveRequest;
use App\Service\User\UserCategoryService;
use Hyperf\Di\Annotation\Inject;

class CategoryController extends ApiController
{

    #[Inject]
    protected UserCategoryService $userCategoryService;


    /**
     * 批量保存用户类目
     * @param CategorySaveRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function batchSave(CategorySaveRequest $request)
    {
        $data = $request->validated();

        $this->userCategoryService->batchSave($data);

        return $this->responseOk();
    }

    /**
     * 获取所有类目
     * @param CategoryDataRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function allCategory(CategoryDataRequest $request)
    {
        $data = $request->validated();

        $type = $data['type'] ?? null;
        $categoryData = $this->userCategoryService->categoryData((int)$type);
        return $this->responseOk($categoryData);
    }
    /**
     * 保存用户类目
     * @param CategoryEditRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function saveCategory(CategoryEditRequest $request)  {
        $data = $request->validated();
        $this->userCategoryService->saveCategory($data);
        return $this->responseOk();
    }

    /**
     * 类目删除
     * @param CategoryRemoveRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function remove(CategoryRemoveRequest $request)
    {
        $data = $request->validated();

        $this->userCategoryService->removeCategory($data['id']);

        return $this->responseOk();
    }
}